package com.tlibrary.service;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tlibrary.dao.CollegeMapper;
import com.tlibrary.dao.UserMapper;
import com.tlibrary.model.College;
import com.tlibrary.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author：wcc
 * @Description：
 * @Date：Create in: 2019/1/9--17:24
 */
@Service
public class AdminService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CollegeMapper cm;
    public HashMap finduser(Integer pageNum, Integer pageSize, HashMap map) {

        PageHelper.startPage(pageNum, pageSize);
        List<Map<String,String>> users=userMapper.getUsers(map);
        PageInfo<Map<String,String>> pageInfo = new PageInfo<Map<String,String>>(users);
        HashMap returnMap = new HashMap();
        returnMap.put("total", pageInfo.getTotal());
        returnMap.put("rows", pageInfo.getList());

        return returnMap;
    }

    public String saveAdduser(User user) {
        user.setId(UUID.randomUUID().toString());
        user.setPassword(user.getAccountid());
        int i=0;
        try {
             i = userMapper.insertSelective(user);
        }catch (Exception e){
            e.printStackTrace();
            return "添加失败";
        }
        if (i==1){
            return "添加成功";
        }else {
            return "添加失败";
        }
    }

    public int delUser(JSONArray array) {
        int msg=0;
        for (int i = 0; i <array.size() ; i++) {
            Map<String,String> o = (Map) array.get(i);
            String id=o.get("id");
            msg+=userMapper.deleteByPrimaryKey(id);
        }
        return msg;
    }

    public int addeUsers(List list) {
        int msg=0;
        for (int i = 0; i <list.size() ; i++) {
            Map<String,String> map= (Map<String, String>) list.get(i);
            User user=new User();
            user.setId(UUID.randomUUID().toString());
            user.setAccountid(map.get("accountid"));
            user.setPassword(map.get("password"));
            user.setUsername(map.get("username"));
            user.setType("1");
             msg += userMapper.insertSelective(user);
        }
        return  msg;
    }
    public HashMap findDept(Integer pageNum, Integer pageSize, HashMap hashMap) {
        PageHelper.startPage(pageNum, pageSize);
        HashMap returnMap=new HashMap();
        List<Map<String,String>> users=cm.getdept(hashMap);
        PageInfo<Map<String,String>> pageInfo = new PageInfo<Map<String,String>>(users);
        returnMap.put("total", pageInfo.getTotal());
        returnMap.put("rows", pageInfo.getList());
        return returnMap;
    }

    public HashMap findCol(Integer pageNum, Integer pageSize, HashMap hashMap) {
        HashMap returnMap = new HashMap();
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String,String>> users=cm.getcol(hashMap);
        PageInfo<Map<String,String>> pageInfo = new PageInfo<Map<String,String>>(users);
        returnMap.put("total", pageInfo.getTotal());
        returnMap.put("rows", pageInfo.getList());
        return returnMap;
    }
    public void addCol(String colname, String userid) throws Exception {
        College college=new College();
        college.setByuserid(userid);
        college.setCollege(colname);
        college.setTime(new Date());
        college.setCollegeid(UUID.randomUUID().toString());
            cm.insert(college);
    }

    public int editcol(String colname, String colnameid,String userid) throws Exception {
        College college=new College();
        college.setByuserid(userid);
        college.setCollege(colname);
        college.setTime(new Date());
        college.setCollegeid(colnameid);
       int i= cm.edit(college);
       return i;
    }

    public void delCol(JSONArray array) throws Exception{
        for (int i = 0; i <array.size() ; i++) {
            Map<String,String> o = (Map) array.get(i);
            String id=o.get("ID");
            cm.deleteColByid(id);
        }
    }

    public void addDept(HashMap hashMap) throws Exception {
        hashMap.put("time",new Date());
        hashMap.put("id",UUID.randomUUID().toString());
        cm.addDept(hashMap);
    }

    public void editDept(HashMap hashMap) throws Exception {
        cm.editDept(hashMap);
    }

    public void deldept(JSONArray array) {
        for (int i = 0; i <array.size() ; i++) {
            Map<String,String> o = (Map) array.get(i);
            String id=o.get("ID");
            cm.deleteDeptByid(id);
        }
    }

    public void recoveryuserPWD(String id) throws  Exception {
        HashMap<String, String> map = new HashMap<>();
        map.put("accountid",id);
        userMapper.recoveryPWDtoAccountid(map);
    }
}
